/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.InBillBean;
import com.minierp.bean.InBillItemBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.BusinessBillDao;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.InBillDao;
import com.minierp.dao.InBillItemDao;
import com.minierp.dao.InTypeDao;
import com.minierp.dao.OutBillItemDao;
import com.minierp.dao.ProductDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.dao.StockBalanceDao;
import com.minierp.dao.StockDao;
import com.minierp.dao.StorehouseDao;
import com.minierp.err.PermissionDennyException;
import com.minierp.err.StockNoEnoughException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.EncInBillFormCtrHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.CodeRule;
import com.minierp.model.InBill;
import com.minierp.model.InBillItem;
import com.minierp.model.OutBill;
import com.minierp.model.OutBillItem;
import com.minierp.model.Product;
import com.minierp.model.Storehouse;
import com.minierp.model.TallySet;
import com.minierp.service.InBillService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

public class InBillFormCtr extends SimpleFormController {
	private InTypeDao inTypeDao;
	private InBillItemDao inBillItemDao;
	private OutBillItemDao outBillItemDao;
	private InBillDao inBillDao;
	private CodeRuleDao codeRuleDao;
	private SerialNumberDao serialNumberDao;
	private StorehouseDao storehouseDao;
	private ProductDao productDao;
	private StockDao stockDao;
	private StockBalanceDao stockBalanceDao;
	private BusinessBillDao businessBillDao;
	private IdGenerator idGenerator;
	private InBillService inBillService;
	private DataSource dataSource;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		refData.put("alertType", request.getParameter("alert_type"));
		refData.put("inTypeList", this.inTypeDao.getList(false));
		refData.put("storehouseList", this.storehouseDao.getList(false));
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		CodeRule cr = this.codeRuleDao.get("IN_BILL");
		if (cr == null) {
			throw new Exception("Code rule not found for businessBill");
		} else {
			InBillBean form;
			String storehouseId;
			List managerName;
			if (id != null && !id.equals("")) {
				InBill arg23 = (InBill) this.inBillDao.get(InBill.class,
						Long.parseLong(id));
				if (arg23 == null) {
					throw new Exception("Record not found for id:" + id);
				}

				storehouseId = PageHelper.getRequName(arg23.getOffererId(),
						arg23.getOffererType(), this.inBillDao);
				TallySet arg25 = (TallySet) this.inBillDao.get(TallySet.class,
						arg23.getTallySetId());
				List arg27 = this.inBillItemDao.getList(arg23.getId());
				long[] arg28 = new long[arg27.size()];

				for (int arg30 = 0; arg30 < arg27.size(); ++arg30) {
					arg28[arg30] = ((InBillItem) arg27.get(arg30))
							.getProductId();
				}

				managerName = this.productDao.getList(arg28);
				form = EncInBillFormCtrHelper.doFormBackingObjectEdit(cr,
						arg23, storehouseId, arg25, arg27, managerName);
			} else {
				if (!EncCommonHelper.checkPrivilege(request, "IN_BILL")) {
					throw new PermissionDennyException("Permission Denny");
				}

				String dataType = request.getParameter("data_type");
				if (dataType == null) {
					dataType = "";
				}

				if (dataType.equals("COPY")) {
					storehouseId = request.getParameter("template_id");
					InBill tb = (InBill) this.inBillDao.get(InBill.class,
							Long.parseLong(storehouseId));
					if (tb == null) {
						throw new Exception("Template InBill not found for id "
								+ storehouseId);
					}

					String managerId = PageHelper.getRequName(
							tb.getOffererId(), tb.getOffererType(),
							this.inBillDao);
					TallySet ts = (TallySet) this.inBillDao.get(TallySet.class,
							tb.getTallySetId());
					managerName = this.inBillItemDao.getList(tb.getId());
					long[] ts1 = new long[managerName.size()];

					for (int itemList = 0; itemList < managerName.size(); ++itemList) {
						ts1[itemList] = ((InBillItem) managerName.get(itemList))
								.getProductId();
					}

					List arg32 = this.productDao.getList(ts1);
					form = EncInBillFormCtrHelper.doFormBackingObjectTemplate(
							request, cr, tb, managerId, ts, managerName, arg32);
				} else {
					long arg24 = 0L;
					long arg26 = 0L;
					String arg29 = "";
					TallySet arg31 = null;
					ArrayList arg33 = new ArrayList();
					boolean getDefault = false;
					if (dataType.equals("OUT_BILL")) {
						String ub = request.getParameter("out_bill_id");
						OutBill storehouseList = (OutBill) this.inBillDao.get(
								OutBill.class, Long.parseLong(ub));
						if (storehouseList != null) {
							arg24 = storehouseList.getStorehouseId();
							arg26 = storehouseList.getManagerId();
							arg29 = storehouseList.getManagerName();
							arg31 = (TallySet) this.inBillDao.get(
									TallySet.class,
									storehouseList.getTallySetId());
							List i = this.outBillItemDao.getList(storehouseList
									.getId());

							for (int sto = 0; sto < i.size(); ++sto) {
								OutBillItem outItem = (OutBillItem) i.get(sto);
								InBillItem inItem = new InBillItem();
								InBillItemBean bean = new InBillItemBean();
								inItem.setAmount(outItem.getAmount());
								inItem.setProductId(outItem.getProductId());
								bean.setAmountStr(EncCommonHelper
										.parseBigDecimal(inItem.getAmount()));
								bean.setInBillItem(inItem);
								Product p = (Product) this.inBillDao.get(
										Product.class, inItem.getProductId());
								if (p != null) {
									bean.setProductCode(p.getProductCode());
									bean.setProductName(p.getProductName());
									bean.setUnit(p.getUnit());
								}

								BigDecimal unitPrice = new BigDecimal(0);
								BigDecimal cost = new BigDecimal(0);
								if (storehouseList.getExamined().equals("Y")) {
									if (outItem.getAmount().compareTo(
											new BigDecimal(0)) != 0) {
										unitPrice = outItem.getCost().divide(
												outItem.getAmount(), 4, 4);
									}

									cost = outItem.getCost();
								}

								inItem.setUnitPrice(unitPrice);
								inItem.setCost(cost);
								bean.setUnitPriceStr(EncCommonHelper
										.parseBigDecimal(inItem.getUnitPrice()));
								bean.setStatus("NEW");
								arg33.add(bean);
							}
						}
					} else {
						getDefault = true;
					}

					if (getDefault) {
						UserBean arg34 = EncCommonHelper
								.getCurrentUserAccount(request);
						List arg35 = this.storehouseDao.getList(arg34);

						for (int arg36 = 0; arg36 < arg35.size(); ++arg36) {
							Storehouse arg37 = (Storehouse) arg35.get(0);
							if (arg31 != null) {
								if (arg37.getTallySetId() == arg31.getId()) {
									arg24 = arg37.getId();
									break;
								}
							} else if (arg37.getTallySetId() == arg34.getUser()
									.getDfTsId()) {
								arg24 = arg37.getId();
								break;
							}
						}
					}

					form = EncInBillFormCtrHelper.doFormBackingObjectNew(
							request, cr, arg24, arg26, arg29, arg31, arg33);
				}
			}

			return form;
		}
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
		InBillBean form = (InBillBean) command;
		EncInBillFormCtrHelper.doOnBind(request, form, this.dataSource);
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		String id = request.getParameter("id");
		String examine = request.getParameter("examine");
		String alertMsg = "";
		InBillBean form = (InBillBean) command;
		InBill obj = form.getInBill();
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		if (examine != null && examine.equals("1")) {
			if (!EncCommonHelper.checkPrivilege(request, "IN_EXAM")) {
				throw new Exception("Permission deny!");
			}

			obj.setExamined("Y");
			if (ub.getStaffer() != null) {
				obj.setExaminerId(ub.getStaffer().getId());
				obj.setExaminerName(ub.getStaffer().getStafferName());
			}
		} else {
			obj.setExamined("N");
		}

		if (id != null && !id.equals("")) {
			if (!EncCommonHelper.checkPrivilege(request, "IN_BILL")) {
				throw new Exception("Permission deny!");
			}

			try {
				this.inBillService.txUpdate(form, this.inBillDao,
						this.inBillItemDao, this.stockDao,
						this.stockBalanceDao, this.businessBillDao,
						this.idGenerator);
			} catch (StockNoEnoughException arg13) {
				EncCommonHelper.setAlertMessage(request, arg13.getMessage());
				return this.showForm(request, errors, this.getFormView());
			} catch (CannotAcquireLockException arg14) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			}

			alertMsg = "修改成功！";
		} else {
			try {
				this.inBillService.txInsert(form, this.inBillDao,
						this.inBillItemDao, this.stockDao,
						this.stockBalanceDao, this.serialNumberDao,
						this.businessBillDao, this.idGenerator);
			} catch (StockNoEnoughException arg15) {
				EncCommonHelper.setAlertMessage(request, arg15.getMessage());
				return this.showForm(request, errors, this.getFormView());
			} catch (CannotAcquireLockException arg16) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			}

			alertMsg = "增加成功！";
		}

		mv.addObject("returnValue", String.valueOf(obj.getId()));
		String continueFlag = request.getParameter("continue_flag");
		String alertType = request.getParameter("alert_type");
		if (continueFlag != null && continueFlag.equals("1")) {
			EncCommonHelper.setAlertMessage(request, alertMsg);
			mv = new ModelAndView(
					"redirect:/storehouse/InBillNew.ihtm?alert_type="
							+ alertType);
		} else if (continueFlag != null && continueFlag.equals("2")) {
			EncCommonHelper.setAlertMessage(request, alertMsg);
			mv = new ModelAndView(
					"redirect:/storehouse/InBillNew.ihtm?alert_type="
							+ alertType + "&data_type=COPY&template_id="
							+ obj.getId());
		} else if (alertType == null || !alertType.equals("NONE")) {
			EncCommonHelper.setAlertMessage(request, alertMsg);
			if (alertType != null && alertType.equals("FORM")) {
				mv = new ModelAndView(new RedirectView(
						"/storehouse/InBillEdit.ihtm?id=" + obj.getId()
								+ "&alert_type=" + alertType));
			}
		}

		mv.addObject("alertType", alertType);
		return mv;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}

	public SerialNumberDao getSerialNumberDao() {
		return this.serialNumberDao;
	}

	public void setSerialNumberDao(SerialNumberDao serialNumberDao) {
		this.serialNumberDao = serialNumberDao;
	}

	public ProductDao getProductDao() {
		return this.productDao;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	public DataSource getDataSource() {
		return this.dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public InBillDao getInBillDao() {
		return this.inBillDao;
	}

	public void setInBillDao(InBillDao inBillDao) {
		this.inBillDao = inBillDao;
	}

	public InBillItemDao getInBillItemDao() {
		return this.inBillItemDao;
	}

	public void setInBillItemDao(InBillItemDao inBillItemDao) {
		this.inBillItemDao = inBillItemDao;
	}

	public InBillService getInBillService() {
		return this.inBillService;
	}

	public void setInBillService(InBillService inBillService) {
		this.inBillService = inBillService;
	}

	public InTypeDao getInTypeDao() {
		return this.inTypeDao;
	}

	public void setInTypeDao(InTypeDao inTypeDao) {
		this.inTypeDao = inTypeDao;
	}

	public StorehouseDao getStorehouseDao() {
		return this.storehouseDao;
	}

	public void setStorehouseDao(StorehouseDao storehouseDao) {
		this.storehouseDao = storehouseDao;
	}

	public StockDao getStockDao() {
		return this.stockDao;
	}

	public void setStockDao(StockDao stockDao) {
		this.stockDao = stockDao;
	}

	public StockBalanceDao getStockBalanceDao() {
		return this.stockBalanceDao;
	}

	public void setStockBalanceDao(StockBalanceDao stockBalanceDao) {
		this.stockBalanceDao = stockBalanceDao;
	}

	public BusinessBillDao getBusinessBillDao() {
		return this.businessBillDao;
	}

	public void setBusinessBillDao(BusinessBillDao businessBillDao) {
		this.businessBillDao = businessBillDao;
	}

	public OutBillItemDao getOutBillItemDao() {
		return this.outBillItemDao;
	}

	public void setOutBillItemDao(OutBillItemDao outBillItemDao) {
		this.outBillItemDao = outBillItemDao;
	}
}